package cc.ttiot.service.dataTransmission;

import cc.ttiot.service.dataTransmission.mqtt.Client;
import com.alibaba.fastjson.JSON;
import org.eclipse.paho.client.mqttv3.MqttException;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import static cc.ttiot.enums.SystemConstant.DOOR_ID;


/**
 * @Description: TODO
 * @Author WangSuheng
 * @Date 2021/10/3 23:13
 * @Version 1.0
 */

//泛型解析: https://www.cnblogs.com/kelelipeng/p/11949512.html
public class DataTransmission {
    public static Client client = Client.getInstance();

    //通用传输数据
    //每个类的topic就为类名

    public static void transmit(String topic, Object object) throws MqttException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("doorId", DOOR_ID);
        map.put("msg",object);
        client.publishMessage(topic, JSON.toJSON(map).toString());
    }

    //通用传输数据
    //每个类的topic就为类名
    public static void transmit(Client client, String topic, Object object) throws MqttException {
        client.publishMessage(topic, JSON.toJSON(object).toString());
    }

    //可以考虑对每个类单独写个发送程序，这样效率高一点

}
